package com.ls.lserp.modules.order.service;



import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.dbaccess.Entity.InventoryChangeImperfectDetailEntity;
import com.ls.lserp.dbaccess.Entity.MaterialRemainEntity;
import com.ls.lserp.dbaccess.Entity.ProductionItemEntity;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.order.dao.ReworkDao;
import com.ls.lserp.modules.order.entity.OrderRework;
import com.ls.lserp.modules.produce.dao.CustomizationQualityDao;
import com.ls.lserp.modules.sys.dao.MenuDao;
import com.ls.lserp.modules.sys.utils.UserUtils;


@Service
@Transactional(readOnly = false)
public class ReworkService extends CrudService<ReworkDao,OrderRework>{

	 	@Autowired
	    private ReworkDao reworkDao ;
	 	@Autowired
	    private CustomizationQualityDao customizationQualityDao ;
	 	@Autowired
	    private OrderDao orderDao ;
	 	@Autowired
		private MenuDao menuDao;

		/**
	     * 得到工序信息
	     */
	    public OrderRework getReworkInfo(OrderRework orderRework) throws Exception{
	    	OrderRework orderReworkInfo =  reworkDao.findReworkInfo(orderRework);
	    	orderReworkInfo.setStyleQualityList(reworkDao.findItemList(orderRework));
	    	return orderReworkInfo;
	    }
		
	    /**
	     * 保存换片信息
	     */
	    public void saveChangeImperfectInfo(InventoryChangeImperfectDetailEntity changeImperfectEntity,String reworkReason) throws Exception {
	    	changeImperfectEntity.preInsert();
	    	reworkDao.insertChangeImperfect(changeImperfectEntity);
	    	//如果来源为料头
	    	if ("CHANGEIMPERFECT_SOURCE-MATERIAL_REMAIN".equals(changeImperfectEntity.getSourceKbn())) {
	    		MaterialRemainEntity materialRemainEntity = new MaterialRemainEntity();
	    		materialRemainEntity.setMaterialId(changeImperfectEntity.getMaterialId());
	    		materialRemainEntity.setMaterialQuantity(changeImperfectEntity.getMaterialQuantity());
	    		materialRemainEntity.preUpdate();
	    		//更新料头数量
				menuDao.updateMaterialRemainSub(materialRemainEntity);
			}
	    	//插入工时表数据
			ProductionItemEntity productionItemEntity = new ProductionItemEntity();
			Long productionId = customizationQualityDao.getProductionId(changeImperfectEntity.getOrderId());
			if (productionId != null) {
				productionItemEntity.setProductionId(productionId);
				productionItemEntity.setCreateDateTime(new Date());
				productionItemEntity.setUpdateDateTime(new Date());
				productionItemEntity.setCreateUserId(UserUtils.getUser().getId());
				productionItemEntity.setUpdateUserId(UserUtils.getUser().getId());
				productionItemEntity.setOrderId(changeImperfectEntity.getOrderId());
				productionItemEntity.setOrderNo(changeImperfectEntity.getOrderNo());
				productionItemEntity.setBarCode(changeImperfectEntity.getOrderNo().substring(changeImperfectEntity.getOrderNo().indexOf(".")+1));
				productionItemEntity.setItemId(97L);
				productionItemEntity.setProcessStatus(ResourcesUtil.getConfig("OPERATING_STATUS10"));
				productionItemEntity.setProcessNumber(new BigDecimal(1));
				productionItemEntity.setRemarks(reworkReason);
				orderDao.insertProItem(productionItemEntity);
			}
	    }
	    
	    /**
	     * 获取库存数量
	     * @param materialId
	     * @return
	     */
	    public BigDecimal getInventoryQuantity(long materialId){
	    	return reworkDao.getInventoryQuantity(materialId);
	    }
	    
	    /**
	     * 获取料头数量
	     * @param materialId
	     * @return
	     */
	    public BigDecimal getRemainQuantity(long materialId){
	    	return reworkDao.getRemainQuantity(materialId);
	    }
}
